<script>
import {formatTime} from "@/utils/formatTime.js";
import {all, edit, add, filter} from '@/api/orders.js'

export default {
  name: "orders",
  data() {
    return {
      ordersData: [],
      editOrdersDialogVisible: false,
      filterOrdersDialogVisible: false,
      addOrdersDialogVisible: false,
      filterOrdersFormData: {},
      editOrdersFormData: {},
      addOrdersFormData: {},
    }
  },
  methods: {
    formatTime,
    refresh() {
      all().then(res => {
        if (res.code === 200) {
          this.ordersData = res.data
        } else {
          this.$message.error("获取数据失败")
        }
      })
    },
    handleEdit(data) {
      this.editOrdersFormData = data;
      this.editOrdersDialogVisible = true;
    },
    editOrders(data) {
      edit(data).then(res => {
        if (res.code === 200) {
          this.$message.success("修改成功")
          this.refresh()
          this.editOrdersDialogVisible = false
          this.editOrdersFormData = {}
        } else {
          this.$message.error("修改失败")
        }
      })
    },
    addOrders(data) {
      add(data).then(res => {
        if (res.code === 200) {
          this.$message.success("添加成功")
          this.refresh()
          this.addOrdersDialogVisible = false
          this.addOrdersFormData = {}
        } else {
          this.$message.error(res.data)
        }
      })
    },
    filterOrders(data) {
      filter(data).then(res => {
        if (res.code === 200) {
          this.ordersData = res.data
          this.filterOrdersDialogVisible = false
        } else {
          this.$message.error(res.data)
        }
      })
    }
  },
  created() {
    this.refresh()
  }
}
</script>
<template>
  <div id="orders-title">
    订单管理
  </div>
  <div id="orders-box">
    <div id="orders-form">
      <el-button type="primary" @click="filterOrdersDialogVisible = true">增加检索</el-button>
      <el-button type="primary" @click="addOrdersDialogVisible = true">新增数据</el-button>
      <el-button type="primary">导入数据</el-button>
      <el-button type="primary">导出数据</el-button>
      <el-button type="primary" @click="refresh">刷新</el-button>
    </div>
    <div>
      <el-table id="user-data" :data="ordersData">
        <el-table-column type="selection" width="50px"/>
        <el-table-column label="用户名" prop="name" width="100"></el-table-column>
        <el-table-column label="订单编号" prop="ordersNo" width="200"></el-table-column>
        <el-table-column label="订单状态" prop="status" width="120"></el-table-column>
        <el-table-column label="地址" prop="address" width="300"></el-table-column>
        <el-table-column label="备注" prop="remark" width="200"></el-table-column>
        <el-table-column label="总价格" prop="totalPrice" width="100"></el-table-column>
        <el-table-column label="手机号" prop="phone" width="150"></el-table-column>
        <el-table-column label="送货方式" prop="pick" width="100"></el-table-column>
        <el-table-column label="物流状态" prop="action" width="100"></el-table-column>
        <el-table-column label="购买数量" prop="quantity" width="100"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="200">
          <template #default="scope">
            <span>{{ formatTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" prop="updateTime" width="200">
          <template #default="scope">
            <span>{{ formatTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="400">
          <template #default="scope">
            <el-button @click="handleEdit(scope.row)" type="primary">编辑</el-button>
            <el-button @click="handleDelete(scope.row.id)" type="danger" disabled>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 修改表单 -->
    <div>
      <el-dialog title="修改订单信息" v-model="editOrdersDialogVisible" :close-on-click-modal="false">
        <el-form :model="editOrdersFormData">
          <el-form-item label="订单号">
            <el-input v-model="editOrdersFormData.ordersNo" disabled></el-input>
          </el-form-item>
          <el-form-item label="总价格">
            <el-input v-model="editOrdersFormData.totalPrice" disabled></el-input>
          </el-form-item>
          <el-form-item label="送货方式">
            <el-select v-model="editOrdersFormData.pick" placeholder="请选择">
              <el-option label="送货上门" value="送货上门"></el-option>
              <el-option label="自提" value="自提"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="物流状态">
            <el-select v-model="editOrdersFormData.action" placeholder="请选择">
              <el-option label="发货中" value="发货中"></el-option>
              <el-option label="送达" value="送达"></el-option>
              <el-option label="已取消" value="已取消"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="订单状态">
            <el-select v-model="editOrdersFormData.status" placeholder="请选择">
              <el-option label="新下单待发货" value="新下单待发货"></el-option>
              <el-option label="已收货待评价" value="已收货待评价"></el-option>
              <el-option label="订单已完成" value="订单已完成"></el-option>
              <el-option label="订单已取消" value="订单已取消"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="editOrdersFormData.address"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="editOrdersFormData.phone" disabled></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="editOrdersFormData.remark"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="editOrdersDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="editOrders(editOrdersFormData)">提交</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>

    <!-- 添加表单 -->
    <div>
      <el-dialog title="添加订单" v-model="addOrdersDialogVisible" :destroy-on-close="true"
                 :close-on-click-modal="false">
        <el-form :model="addOrdersFormData">
          <el-form-item label="用户ID">
            <el-input v-model="addOrdersFormData.userId"></el-input>
          </el-form-item>
          <el-form-item label="商品ID">
            <el-input v-model="addOrdersFormData.goodsId"></el-input>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="addOrdersFormData.address"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="addOrdersFormData.remark"></el-input>
          </el-form-item>
          <el-form-item label="购买数量">
            <el-input v-model="addOrdersFormData.quantity"></el-input>
          </el-form-item>
          <el-form-item label="送货方式">
            <el-select v-model="addOrdersFormData.pick" placeholder="请选择">
              <el-option label="送货上门" value="送货上门"></el-option>
              <el-option label="自提" value="自提"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-form-item>
              <el-button type="info" @click="addOrdersDialogVisible = false">取消</el-button>
              <el-button type="primary" @click="addOrders(addOrdersFormData)">提交</el-button>
            </el-form-item>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
  <!-- 过滤  -->
  <div>
    <el-dialog title="条件筛选" v-model="filterOrdersDialogVisible" :close-on-click-modal="false">
      <el-form :model="filterOrdersFormData">
        <el-form-item label="订单号">
          <el-input v-model="filterOrdersFormData.ordersNo"></el-input>
        </el-form-item>
        <el-form-item label="送货方式">
          <el-select v-model="filterOrdersFormData.pick" placeholder="请选择">
            <el-option label="送货上门" value="0"></el-option>
            <el-option label="自提" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物流状态">
          <el-select v-model="filterOrdersFormData.action" placeholder="请选择">
            <el-option label="发货中" value="seller"></el-option>
            <el-option label="送达" value="songda"></el-option>
            <el-option label="已取消" value="quxiao"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="filterOrdersFormData.status" placeholder="请选择">
            <el-option label="新下单待发货" value="新下单待发货"></el-option>
            <el-option label="已收货待评价" value="已收货待评价"></el-option>
            <el-option label="订单已完成" value="订单已完成"></el-option>
            <el-option label="订单已取消" value="订单已取消"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="手机号">
          <el-input v-model="filterOrdersFormData.phone"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="filterOrdersDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="filterOrders(filterOrdersFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

</template>

<style scoped>
#orders-box {
  background-color: #68a273;
  width: 90%;
  height: 90%;
  margin-top: 2%;
  margin-left: 3%;
  border-radius: 30px;
}

#orders-title {
  font-size: 30px;
  margin-left: 20px;
}

#orders-form {
  display: flex;
  padding-top: 40px;
  justify-content: flex-end;
  margin-right: 80px;
  margin-bottom: 10px;
}
</style>